// AppID：wxe6f2423f5a23d7b1
// AppSecret：6baf4ddfcd7541e6d94a72098409d4db
//高德地图 appkey:
// android:8728e65b009ec40ac73f4bb3d03fb08c
// ios:5ad3ea0f81eaea20a0f56404e2a34d4c
let version;
// 配置API接口地址
let URL;
let URL1;
let URL2 = 'https://letsp.tspsp.com/api';
let WebSocket_URL;

const img_url = 'https://y2t-nfp-dev-1258746579.cos.ap-guangzhou.myqcloud.com/imgApp';

let Interval;
if (process.env.NODE_ENV === 'development') { // 开发环境
	//本地开发调试
	// URL = 'http://159.75.206.134:8194';
	// URL1 = 'http://159.75.206.134';
	// URL = 'http://159.75.206.134:81/to_y2t_ready';
	//测试https:
	// URL = 'http://nfptest.y2t.com/miniapi';
	// URL1 = 'http://nfptest.y2t.com'
	//准生产环境
	// URL = 'http://nfpready.y2t.com/miniapi'
	// URL1 = 'http://nfpready.y2t.com'
	// WebSocket_URL = 'ws://nfpready.y2t.com/customer/msg'
	// 正式发布
	URL = 'https://nfp.y2t.com/miniapi';
	URL1 = 'https://nfp.y2t.com';
	WebSocket_URL = 'wss://nfp.y2t.com/customer/msg'
} else { // 生产环境
	//本地开发调试
	// URL = 'http://159.75.206.134:8194';
	// URL1 = 'http://159.75.206.134';
	// URL = 'http://159.75.206.134:81/to_y2t_ready';
	//测试
	// URL = 'http://nfptest.y2t.com/miniapi';
	// URL1 = 'http://nfptest.y2t.com'
	//准生产环境
	// URL = 'http://nfpready.y2t.com/miniapi'
	// URL1 = 'http://nfpready.y2t.com'
	// WebSocket_URL = 'ws://nfpready.y2t.com/customer/msg'
	// 正式发布
	URL = 'https://nfp.y2t.com/miniapi';
	URL1 = 'https://nfp.y2t.com';
	WebSocket_URL = 'wss://nfp.y2t.com/customer/msg'
}

function jumpFlag(data) { //跳转开关
	if (getApp().globalData.is_flag) { //is_flag开始等于 true
		getApp().globalData.is_flag = false;
		uni.navigateTo({
			url: data.url,
		});
		setTimeout(() => {
			getApp().globalData.is_flag = true;
			console.log(getApp().globalData.is_flag);
		}, 2000)
	} else {
		console.log('重复点击了');
	}
}


/**
 * 网络请求
 * @param {Object} obj对象与uni.request的OBJECT参数一样，额外增了几个参数。
 * @param {Object.title} obj = [请求时等待提示语] 
 */
// import CryptoJS from 'crypto-js'
var app = getApp()
function request(obj) {
	if (!obj) {
		uni.showToast({
			title: '请求参数不能为空',
			icon: 'none'
		});
		return;
	}

	if (obj.title) {
		uni.showLoading({
			title: obj.title
		});
	}
	let header = {}
	if (uni.getStorageSync(this.DATA_KEY.token) != '') {
		header = {
			'authorization': uni.getStorageSync(this.DATA_KEY.token),
		}
	}
	console.log(obj.url,1111);
	uni.request({
		url: obj.url,
		method: obj.method,
		header: header,
		data: JSON.stringify(obj.data),
		success: res => {
			// console.log('请求', JSON.stringify(obj));
			// console.log('返回res', res);
			obj.title && uni.hideLoading();
			if (res.data.code == 0) {
				let respost = JSON.parse(JSON.stringify(res).replace(/:null/g, ':""'));
				// console.log('返回', obj.url + '  :  ' + JSON.stringify(respost));
				obj.success(respost);
			} else if(res.data.code == 1){
				
				//电子签注册认证成功返回状态code为1
				//http://nfpready.y2t.com/seal/driverNotify/authSign判断是否准册认证
				if(obj.url == `${URL1}/seal/driverNotify/authSign` || obj.url.includes('driverService/checkPlatformDriver')){
					// console.log(111)
					
					let respost = JSON.parse(JSON.stringify(res).replace(/:null/g, ':""'));
					// console.log('返回', obj.url + '  :  ' + JSON.stringify(respost));
					obj.success(respost);
				}else{
					if (res.data.msg && res.data.msg.length >= 23) { // 错误提示过长问题
						uni.showModal({
							title: '提示',
							content: res.data.msg,
							showCancel: false,
							confirmText: '确认',
							success: res => {
								console.log(res)
							}
						})
					} else {
						uni.showToast({
							title: res.data.msg,
							icon: 'none',
							duration: 2000
						});
					}
				}
				
				
			} else if (res.data.code == 9000) {
				if (!getApp().globalData.isAuthAgain && uni.getStorageSync('user-info').mobile) {
					getApp().globalData.isAuthAgain = true
					uni.showModal({
						title: '温馨提示',
						content: '您的登录状态已过期，请重新登录',
						showCancel: false,
						confirmText: '重新登录',
						success(res) {
							if (res.confirm) {
								// #ifdef MP-WEIXIN
								uni.navigateTo({
									url: '/pages/personal/login',
									success: () => {
										uni.setStorageSync('user-info', '');
										uni.setStorageSync('token', '');
										uni.setStorageSync('etpId', '');
										uni.setStorageSync('etpName', '');
										uni.setStorageSync('oldTime', '');
										uni.setStorageSync('loadingOrderId', '');
										uni.removeStorageSync('loginType');
										getApp().globalData.isAuthAgain = false
									}
								})
								// #endif
								// #ifndef MP-WEIXIN
								uni.navigateTo({
									url: '/pages/personal/login',
									success: () => {
										uni.setStorageSync('user-info', '');
										uni.setStorageSync('token', '');
										uni.setStorageSync('etpId', '');
										uni.setStorageSync('etpName', '');
										uni.setStorageSync('oldTime', '');
										uni.setStorageSync('loadingOrderId', '');
										uni.removeStorageSync('loginType');
										getApp().globalData.isAuthAgain = false
									}
								})
								// #endif
							}
						}	
					})
				}
				
			} else{
				if (res.data.msg && res.data.msg.length >= 23) { // 错误提示过长问题
					uni.showModal({
						title: '提示',
						content: res.data.msg,
						showCancel: false,
						confirmText: '确认',
						success: res => {
							console.log(res)
						}
					})
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none',
						duration: 2000
					});
				}
				// #ifdef MP-WEIXIN
				// uni.showToast({
				// 	title: res.data.msg,
				// 	icon: 'none',
				// 	duration: 2000
				// });
				// #endif
				// #ifdef APP-PLUS
				// this.$Toast(res.data.msg, {duration: 2000})
				// #endif
			}
		},
		fail: error => {
			console.log('fail ', '错误');
			uni.hideLoading();
			uni.showToast({
				title: '网络异常！',
				icon: 'none'
			});
			if (obj.fail) obj.fail(error);
		},

		complete: (res) => {
			if (obj.complete) obj.complete(res);
		}
	});
}

function request2(obj) {
	if (!obj) {
		uni.showToast({
			title: '请求参数不能为空',
			icon: 'none'
		});
		return;
	}

	if (obj.title) {
		uni.showLoading({
			title: obj.title
		});
	}

	let header = {}
	uni.request({
		url: obj.url,
		method: obj.method,
		header: header,
		data: JSON.stringify(obj.data),
		success: res => {
			// console.log('请求', JSON.stringify(obj));
			console.log('返回', res);
			// console.log('返回', obj.url + '  :  ' + JSON.stringify(res));
			uni.hideLoading();
			obj.success(res);
		},

		fail: error => {
			console.log('返回', error);
			uni.hideLoading();
			uni.showToast({
				title: '网络异常！',
				icon: 'none'
			});
			if (obj.fail) obj.fail(error);
		},

		complete: (res) => {
			if (obj.complete) obj.complete(res);
		}
	});
}

// 本地保存数据的KEY
let DataKey = {
	// 用户信息
	userInfo: 'user-info',
	token: 'token',
	idPhotoUp: 'idPhotoUp',
	idPhotoDown: 'idPhotoDown',
	etpId: 'etpId',
	etpName: 'etpName',
	oldTime: 'oldTime',
	billNo: 'billNo',
	signState: 'signState',
	loadingOrderId: 'loadingOrderId',
	// 是否已启动过程序
	isLaunched: 'isLaunched',
	servinghint:'servinghint',
	platformCoNo: 'platformCoNo',
	
	orderNumber: 'orderNumber',
	accessToken: 'accessToken',
	liveFlag: 'liveFlag',
	
	scanUrl: 'scanUrl',
	
	timeOld: 'timeOld',
	waybillId: 'waybillId',
	taskvalue: 'taskvalue',
	
	shipAuthFlag: 'shipAuthFlag', // 无船个人信息去认证
	backAccountFlag: 'backAccountFlag', // 是否从其他程序返回找回账号页面
	
	shipSignFlag: 'shipSignFlag', // 无船首页去签约状态
}

module.exports = {
	version,
	URL,
	URL1,
	URL2,
	DataKey,
	request,
	request2,
	jumpFlag,
	Interval,
	img_url,
	WebSocket_URL
}
